#include "CQComboBoxDelegate.h"

#include <QComboBox>

CQComboBoxDelegate::CQComboBoxDelegate(QObject *parent) :QStyledItemDelegate(parent)
{

}

QWidget *CQComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QComboBox *cmbSex = new QComboBox(parent);
    cmbSex->insertItem(0, "男");
    cmbSex->insertItem(1, "女");
    return cmbSex;
}

void CQComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    QComboBox *cmbSex = static_cast<QComboBox*>(editor);
    QString sex = index.model()->data(index).toString();
    cmbSex->setCurrentText(sex);
}

void CQComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    QComboBox *cmbSex = static_cast<QComboBox*>(editor);
    QString sex = cmbSex->currentText();
    model->setData(index, sex);
}

void CQComboBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    editor->setGeometry(option.rect);
}
